package admin

import (
	"google.golang.org/grpc"
	"grpc.getaway.adminspro.padmins.com/internal/rpc/utils/grpcServer"
	"grpc.getaway.adminspro.padmins.com/pkg/config"
	pbAdmin "grpc.getaway.adminspro.padmins.com/pkg/proto/admin"
	"strconv"
)

type admin struct {
	rpcPort         int
	rpcAddress      string
	rpcRegisterName string
}

func NewRpcServer(port int) *admin {
	return &admin{
		rpcPort:         port,
		rpcAddress:      config.Config.Rpc.Address.Admin,
		rpcRegisterName: config.Config.Rpc.RegisterName.Admin,
	}
}

func (rpc *admin) Run() {
	if rpc.rpcAddress == "" {
		rpc.rpcAddress = "0.0.0.0:" + strconv.Itoa(rpc.rpcPort)
	}
	grpcServer.NewServer(rpc.rpcRegisterName, rpc.rpcAddress, func(server *grpc.Server) {
		pbAdmin.RegisterAdminServer(server, rpc)
	})
}
